package org.caowei.samples.sevice;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Workbook;
import org.caowei.samples.Utils.ExcelUtils;
import org.caowei.samples.dao.IUserDAO;
import org.caowei.samples.model.User;
import org.caowei.samples.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private IUserDAO IUserDAO;
	@Override
	public List<User> findAllUsers()throws Exception {
		return IUserDAO.findAllUsers();
	}
	
	//根据Excel内容批量添加学生
	@Override
	public void saveStudnetByExcel(InputStream inputStream,String postfix){
		Workbook wk = ExcelUtils.openWorkbook(inputStream,postfix);
		List<UserVO> allExcelUser = ExcelUtils.getStudentListByExcel(wk);
		List<User> entities = new ArrayList<User>();
		if(allExcelUser !=null){
//			ExamRole role = examRoleDAO.get(25);
			User entity = null;
			for(int i = 0;i<allExcelUser.size();i ++){
				entity = new User();
				BeanUtils.copyProperties(allExcelUser.get(i), entity);
//				Set<ExamRole> examRoles = new HashSet<ExamRole>();
//				examRoles.add(role);
//				entity.setExamRoles(examRoles);
				entities.add(entity);
				
			}
		}
		IUserDAO.save(entities);
	}
	
	
	@Override
	public void createUser(User user) throws Exception {
		IUserDAO.createUser(user);
	}
	@Override
	public void editUser(User user) throws Exception {
		IUserDAO.editUser(user);
	}
	@Override
	public User getUserById(String id)throws Exception {
		return IUserDAO.getUserById(id);
	}
	@Override
	public void deleteUser(String id) throws Exception {
		IUserDAO.deleteUser(id);
	}

	@Override
	public void updateUserLoginFailCout(UserVO resultVO) throws Exception {
		User user=new User();
		BeanUtils.copyProperties(resultVO, user);
		IUserDAO.editUser(user);
		
	}

	@Override
	public void updateUserLastLockTime(UserVO resultVO) throws Exception {
		User user=new User();
		BeanUtils.copyProperties(resultVO, user);
		IUserDAO.editUser(user);
		
	}

	@Override
	public UserVO getloginById(String id) throws Exception {
		return IUserDAO.getloginById(id);
	}

}
